package com.agileboot.domain.system.dict.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.system.dict.command.DictTypeAddCommand;
import com.agileboot.domain.system.dict.command.DictTypeUpdateCommand;
import com.agileboot.domain.system.dict.db.SysDictTypeEntity;
import com.agileboot.domain.system.dict.db.SysDictTypeService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class DictTypeModel extends SysDictTypeEntity {

    private SysDictTypeService dictTypeService;

    public DictTypeModel(SysDictTypeEntity entity,SysDictTypeService dictTypeService){
        this(dictTypeService);
        if(entity != null){
            BeanUtil.copyProperties(entity,this);
        }
    }

    public DictTypeModel(SysDictTypeService dictTypeService) {this.dictTypeService=dictTypeService;}

    public void loadDictTypeAdd(DictTypeAddCommand command){
        if(command != null){
            loadDictTypeAdd(command);
        }
    }

    public void loadDictTypeUpdate(DictTypeUpdateCommand command){
        if(command != null){
            loadDictTypeAdd(command);
        }
    }

    /**
     * 检测字典名称是否重复
     */
    public void checkDictNameIsUnique() {
        if (StrUtil.isNotEmpty(getDictName()) && dictTypeService.isParameterDuplicated(getDictName(),
                "dictName",getDictId(),"dictId")) {
            throw new ApiException(ErrorCode.Business.DICT_NAME_NOT_REPEAT);
        }
    }

    /**
     * 检测字典类型是否重复
     */
    public void checkDictTypeIsUnique() {
        if (StrUtil.isNotEmpty(getDictType()) && dictTypeService.isParameterDuplicated(getDictType(),
                "dictType",getDictId(),"dictId")) {
            throw new ApiException(ErrorCode.Business.DICT_TYPE_NOT_REPEAT);
        }
    }
}
